﻿namespace Communication.Help.Crc;

// 基础架构类（可放在单独文件）
internal static class Crc16Utils
{
    internal static ushort[] GenerateTable(ushort poly, bool refIn)
    {
        var table = new ushort[256];
        for (var i = 0; i < 256; i++)
        {
            var crc = refIn ? (ushort)i : (ushort)(i << 8);
            for (var j = 0; j < 8; j++)
                crc = refIn
                    ? (ushort)((crc & 1) != 0 ? (crc >> 1) ^ poly : crc >> 1)
                    : (ushort)((crc & 0x8000) != 0 ? (crc << 1) ^ poly : crc << 1);
            table[i] = crc;
        }

        return table;
    }
}